<?php

/**
 * This is the model class for table "project_nationality".
 *
 * The followings are the available columns in table 'project_nationality':
 * @property integer $id
 * @property integer $project_id
 * @property integer $nationality_id
 * @property integer $active
 * @property integer $delmark
 * @property string $created_date
 * @property string $updated_date
 *
 * The followings are the available model relations:
 * @property Project $project
 * @property Nationality $nationality
 */
class ProjectNationality extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ProjectNationality the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'project_nationality';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('project_id, nationality_id', 'required'),
            array('project_id, nationality_id, active, delmark', 'numerical', 'integerOnly'=>true),
            array('created_date, updated_date', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, project_id, nationality_id, active, delmark, created_date, updated_date', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
            'nationality' => array(self::BELONGS_TO, 'Nationality', 'nationality_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'project_id' => 'Project',
            'nationality_id' => 'Nationality',
            'active' => 'Active',
            'delmark' => 'Delmark',
            'created_date' => 'Created Date',
            'updated_date' => 'Updated Date',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('project_id',$this->project_id);
		$criteria->compare('nationality_id',$this->nationality_id);
		$criteria->compare('active',$this->active);
		$criteria->compare('delmark',$this->delmark);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_date',$this->updated_date,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * before save
     */
    public function beforeSave() {
        if ($this->isNewRecord)
            $this->created_date = new CDbExpression('NOW()');
        else
            $this->updated_date = new CDbExpression('NOW()');

        return parent::beforeSave();
    }

    /**
     * get Delmark off
     * @return array()
     */
    public function getAll()
    {
        return $this->findAll('delmark=:delmark', array(':delmark' => false));
    }

    /**
     * get Delmark off and Active on
     * @return array()
     */
    public function getAllActive()
    {
        return $this->findAll('delmark=:delmark AND active=:active', array(
                   ':delmark' => false,
                   ':active'  => true,
               ));
    }

    /**
     * virtual field : active
     * @return boolean
     */
    public function getActiveFlag()
    {
        return $this->active==1 ? 'true' : 'false';
    }

    /**
     * get By Project ID
     * @return array
     */
    public function getByProjectId($projectId)
    {
        $models = $this->with('nationality')->findAll(
                    't.project_id = :projectId AND
                     t.active = :active AND
                     nationality.active = :active',
                array(':projectId' => $projectId,
                        ':active'    => '1',
                )
        );
        $ret = array();
        if($models) {
            foreach($models as $model) {
                $ret[] = $model->nationality['country'];
            }
        }
        return $ret;
    }

    /**
     * get By Project ID
     * @return array
     */
    public function getActiveByProjectId($projectId)
    {
        if(isset($projectId)) {

        return $this->with('nationality')->findAll(
                't.project_id = :projectId AND
                     t.active = :active AND
                     t.delmark = :delmark AND
                     nationality.active = :active',
                array(':projectId' => $projectId,
                        ':active'  => '1',
                       ':delmark'  => '0',
                )
        );
        }
        return array();
    }

    /**
     * get By Project ID & Nationality Id
     * @param array
     * @return object
     */
    public function getByProjectIdAndNationalityId($projectId,$nationalityId)
    {
        return $this->find('project_id = :projectId AND nationality_id = :nationalityId',
                array(':projectId'     => $projectId,
                      ':nationalityId' => $nationalityId,
                )
        );

    }

}